package com.hs.srm.api.modules.othersysapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hs.srm.common.utils.R;
import com.sap.conn.jco.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author socik
 * @date 2019/11/28 8:39
 * @email socik@qq.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "SAP底价维护接口")
public class PricesConditions {

    @ApiOperation(value = "SAP底价维护", notes = "传入MODE,IN_TAB")
    @GetMapping("/pricesConditions/{jsonArray}")
    public R getCrmData(@ApiParam(value = "传入MODE,IN_TAB一个JSON数组") @PathVariable("jsonArray") String jsonArray) {
        //把传过来的json格式转换成map
        JSONObject jsonObject = JSON.parseObject(jsonArray);
        JSONObject MODE= JSON.parseObject(CreditlimitChangeNew.tirmFristAndLast(jsonObject.getString("MODE")));
        JSONObject INTAB= JSON.parseObject(CreditlimitChangeNew.tirmFristAndLast(jsonObject.getString("IN_TAB")));
        Map<String, Object> mapMode = MODE;
        Map<String, Object> mapINTAB = INTAB;
        JCoDestination destination = null;
        JCoFunction function = null;
        JCoParameterList list = null;
        JCoParameterList ImportList = null;
        JCoParameterList exportList=null;
        try {
            destination = JCoDestinationManager.getDestination("IDOC_AFS");
            function = destination.getRepository().getFunction("ZFG_CRM_BAPI_PRICES_CONDITIONS");
            list = function.getTableParameterList();
            ImportList=function.getImportParameterList();
            exportList=function.getExportParameterList();
        } catch (JCoException e) {
            e.printStackTrace();
        }
        //传入参数数据
        mapMode.forEach(ImportList::setValue);
        //传入表数据
        JCoTable table=list.getTable("IN_TAB");
        table.appendRow();
        mapINTAB.forEach(table::setValue);
        try {
            function.execute(destination);
        } catch (JCoException e) {
            e.printStackTrace();
        }
        //返回参数数据
        String typeMsg=exportList.getString("G_TYPE")+exportList.getString("G_ZMSG");
        return  R.msg(typeMsg);
    }

}
